package whf.framework.workflow.client;

import java.io.Serializable;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import uk.ltd.getahead.dwr.WebContextFactory;
import whf.framework.security.UserContext;
import whf.framework.web.WebContextHolder;
import whf.framework.workflow.engine.WorkflowEntityService;
import whf.framework.workflow.entity.WorkflowEntity;

/**
 * @author king
 * @modify May 16, 2008 2:29:53 PM
 * 
 */
@SuppressWarnings("serial")
public class WorkflowDWRClient implements Serializable {

	public void handle(String entityName, long id, String action, String transitionName, Map<String, String> params) throws Exception {
		HttpServletRequest request = WebContextFactory.get().getHttpServletRequest();
		UserContext userContext = WebContextHolder.getWebContext().getUserContext();
		WorkflowEntity entity = WorkflowEntityService.findWorkflowEntity(entityName, id);
		WorkflowClient workflowClient =  WorkflowClient.getWorkflowClient(entity, userContext.getUser().getUsername());
		workflowClient.processTask(action, transitionName, params);
	}

	public void startProcess(String entityName, long id) throws Exception {
		HttpServletRequest request = WebContextFactory.get().getHttpServletRequest();
		UserContext userContext = WebContextHolder.getWebContext().getUserContext();
		WorkflowEntity entity = WorkflowEntityService.findWorkflowEntity(entityName, id);
		WorkflowClient workflowClient =  WorkflowClient.getWorkflowClient(entity, userContext.getUser().getUsername());
		workflowClient.startProcessInstance();
	}
}
